package learningThread.syn.synchronizedMethodLockObj.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * lock的线程就是持有对象监视器,其他线程只有等待锁释放时再次争抢,效果和syn一样
 */
public class MyService {

    private Lock lock = new ReentrantLock();

    public void testLockMethod(){
        lock.lock();
        for (int i = 0; i < 5; i++) {
            System.out.println("ThreadName="+Thread.currentThread().getName()+(""+(i+1)));
        }
        lock.unlock();
    }
}
